import tkinter as tk
import sqlite3
from tkinter import messagebox
root = tk.Tk()
root.title("Login")
print('已跳转')
def register():
    # 获取新用户名和密码
    new_username = new_username_entry.get()
    new_password = new_password_entry.get()

    # 连接到SQLite数据库并执行注册
    if create_account(new_username, new_password):
        print("注册成功！")
        messagebox.showinfo("成功", "注册成功！")
    else:
        print("注册失败！用户名已存在。")
        messagebox.showinfo("错误", "用户名已存在，请选择其他用户名。")

def create_account(username, password):
    # 连接到SQLite数据库
    conn = sqlite3.connect('music_app.db')
    cursor = conn.cursor()

    # 检查用户名是否已存在
    cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
    existing_user = cursor.fetchone()

    if existing_user:
        conn.close()
        return False

    # 插入新用户到数据库中
    cursor.execute("INSERT INTO users (username, email ,password) VALUES (?,? ,?)", (username,'NULL',password))
    conn.commit()

    # 关闭连接
    conn.close()

    return True
# 创建注册按钮
# 设置窗口大小和位置
root.geometry("300x400")
root.resizable(False, False)
# 设置背景图片
bg_image = tk.PhotoImage(file="login.gif")
background_label = tk.Label(root, image=bg_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
#欢迎登陆字样
username_label = tk.Label(root, text="欢迎注册听歌系统:", font=("Helvetica", 16))
username_label.place(relx=0.5, rely=0.25, anchor="center")

new_username_label = tk.Label(root, text="New Username:", bg="white")
new_username_label.place(relx=0.5, rely=0.34, anchor="center")
new_username_entry = tk.Entry(root, width=30)
new_username_entry.place(relx=0.5, rely=0.40, anchor="center")

new_password_label = tk.Label(root, text="New Password:", bg="white")
new_password_label.place(relx=0.5, rely=0.47, anchor="center")
new_password_entry = tk.Entry(root, width=30, show="*")
new_password_entry.place(relx=0.5, rely=0.53, anchor="center")

register_button = tk.Button(root, text="注册", width=10, command=register, bg="#2196F3", fg="white")
register_button.place(relx=0.5, rely=0.63, anchor="center")



root.mainloop()